<?php
namespace Attendance\Controller;
use Think\Controller;
class Test
{
	public $code;
	public $msg;
	public $count;
	public $data;
}
class Test1
{
    public $date;
    public $typea;
    public $typeb;
}
class Test2
{
    public $value;
    public $name;
}
class IndexController extends Controller {
    public function profile(){
        if((session('status')>0)&&(session('status')!=4))
        {
            $this->display();
        }
        else if(session('status')==4)
        {
            $this->error("您好超级管理员，你没有个人考勤的数据");
        }
        else
        {
            $this->error("权限不足");
        }
    }
    public function admin(){
        if(session('status')>0)
        {
            $this->display();
        }
        else
        {
            $this->error("权限不足");
        }
    }
    public function history(){
        if(session('status')>0)
        {
            $this->display();
        }
        else
        {
            $this->error("权限不足");
        }
    }
    //从数据库获取考勤情况
    public function getmsg($page,$limit){
    	$test=new Test();
    	$user=M('user');
        $start=($page-1)*$limit;
    	//$att=M('Attendance');
    	$t=time();
    	$day=date('Y-m-d',$t);
    	$info=$user->join('join sz_attendance on sz_user.num=sz_attendance.num')->field('sz_user.num,name,group,status,stat')->where("status>0 and date='$day'")->limit($start,$limit)->select();
    	$count=$user->where('status>0')->count();
    	/*for($i=0;$i<$count;$i++)
    	{
    		$att->num=$info[$i]['num'];
    		$att->date='2018-1-1';
    		$att->add();
    	}*/
    	//dump($info);
    	$test->code=0;
    	$test->msg="";
    	$test->count=$count;
    	$test->data=$info;
    	$this->ajaxReturn($test);
    }
    //添加当日人员信息
    public function tianjia()
    {   
        $att=M('Attendance');    
    	$user=M('user');
        $t=date('Y-m-d',time());
        $info3=$att->where("date='$t'")->find();
        if(empty($info3))
        {
            $info1=$user->field('id,num')->where('duty=1')->find();
        	$info=$user->where('status>0 and status!=4')->select();
        	$count=$user->where('status>0 and status!=4')->count();
        	for($i=0;$i<$count;$i++)
        	{
        		$att->num=$info[$i]['num'];
        		$att->date=$t;
        		$att->add();
        	}
            $num=$info1['num'];
            $info2=$att->field('id')->where("num='$num' and date='$t'")->find();
            $att->id=$info2['id'];
            $att->leader=1;
            $att->save();
        }
    }
    //判断数据库是否存在当日人员信息
    public function click()
    {
        $att=M('attendance');
        $t=date('Y-m-d',time());
        $info=$att->where("date='$t'")->find();
        $this->ajaxReturn($info);
    }
    //三个选项
    public function btn1()
    {
    	$att=M('Attendance');
    	$num=I('num');
    	$t=time();
    	$day=date('Y-m-d',$t);
    	$ids=$att->field('id')->where("num='$num' and date='$day'")->find();
    	$id=$ids['id'];
    	$att->id=$id;
    	$att->stat=1;
    	$info=$att->save();
    	$this->ajaxReturn($info);
    }
    public function btn2()
    {
    	$att=M('Attendance');
    	$num=I('num');
    	$t=time();
    	$day=date('Y-m-d',$t);
    	$ids=$att->field('id')->where("num='$num' and date='$day'")->find();
    	$id=$ids['id'];
    	$att->id=$id;
    	$att->stat=-2;
    	$info=$att->save();
    	$this->ajaxReturn($info);
    }
    public function btn3()
    {
    	$att=M('Attendance');
    	$num=I('num');
    	$t=time();
    	$day=date('Y-m-d',$t);
    	$ids=$att->field('id')->where("num='$num' and date='$day'")->find();
    	$id=$ids['id'];
    	$att->id=$id;
    	$att->stat=-1;
    	$info=$att->save();
    	$this->ajaxReturn($info);
    }
    //是否存在考勤员
    public function choose()
    {
        $user=M('user');
        $info=$user->field('id')->where('duty=1')->find();
        if(empty($info))
        {
            $mins=$user->field('id')->where('status>0 and status!=4')->order('id asc')->limit(0,1)->find();
            $min=$mins['id'];
            $user->id=$min;
            $user->duty=1;
            $user->save();
        }
    }
    //提交按钮考勤员轮转
    public function tijiao()
    {
    	$user=M('user');
    	$info=$user->field('id')->where('duty=1')->find();
        $info1=$user->field('id')->where('status=4')->find();
        if((session('id')==$info['id'])||(session('id')==$info1['id']))
        {            
        	/*$mins=$user->where('status>0')->order('id asc')->limit(0,1)->find();
        	$min=$mins['id'];*/            
        	$max=$user->max('id');
        	$id=$info['id'];
        	$user->id=$id;
        	$user->duty=0;
        	$user->save();
        	do{
    	    	$id++;
    	    	/*$info1=$user->field('status')->where("id='$id'")->find();
    	    	if(empty($info1))
    	    	{
    	    		//$id=0;也可，==0表示为空?
    	    		$id=1;
    	    		$info1=$user->field('status')->where("id='$id'")->find();
    	    	}*/
    	    	if($id==$max+1)
    	    	{
    	    		$id=1;
    	    	}
    	    	$info1=$user->field('status')->where("id='$id'")->find();
    	    }
        	while(($info1['status']==0)||($info1['status']==4));
        	$user->id=$id;
        	$user->duty=1;
        	$info2=$user->save();
        }
        else//防止强行进入
        {
            $this->error("权限不足");
        }
    }
    //当日考勤人数
    public function stat()
    {
    	$att=M('attendance');
    	$t=date('Y-m-d',time());
    	$unatt1=$att->where("stat=-2 and date='$t'")->count();
    	$att1=$att->where("(stat=-1||stat=1) and date='$t'")->count();//加个括号，优先级有问题
    	$m[0]=$att1;
    	$m[1]=$unatt1;
    	$this->ajaxReturn($m);
    }
    //判断是否是考勤员
    public function auth()
    {
        $user=M('user');
        $att=M('attendance');
        $day=date('Y-m-d',time());
        $info=$user->field('id,num')->where('duty=1')->find();
        $info1=$att->field('num')->where("leader=1 and date='$day'")->find();
        if((session('id')==$info['id'])&&($info['num']==$info1['num']))
        {
            $this->ajaxReturn(1);
        }
        else if(session('status')==4)
        {
            $this->ajaxReturn(2);
        }
        else
        {
            $this->ajaxReturn(0);
        }
        /*echo session('id');
        echo $info['id'];*/ 
    }
    public function sess()
    {
        $user=M('user');
        $ses=session('id');
        $info=$user->field('id')->where("duty=1")->find();
        $info1=$user->field('id')->where("status=4")->find();
        if(($ses==$info['id'])||($ses==$info1['id']))
        {
            $this->ajaxReturn(1);
        }
    }
    public function history1()
    {
        $test1[]=new Test1();
        $att=M('attendance');
        //$day=date('m-d-Y',time());
        for($i=0;$i<20;$i++)
        {
            $d=20-$i;
            $day=date("Y-m-d",strtotime("-".$d." day"));
            $test1[$i]->date=$day;
            $test1[$i]->typea=$att->where("date='$day' and (stat=-1||stat=1)")->count();
            $test1[$i]->typeb=$att->where("stat=-2 and date='$day'")->count();
        }
        $this->ajaxReturn($test1);
    }
    public function history2($page,$limit)
    {
        $test=new Test();
        $att=M('attendance');
        $user=M('user');
        $start=($page-1)*$limit;
        $info1=$att->join('join sz_user on sz_user.num=sz_attendance.num')->field('date,sz_attendance.num,name,stat,status,group')->limit($start,$limit)->select();
        $count11=$att->join('join sz_user on sz_user.num=sz_attendance.num')->field('date,sz_attendance.num,name,stat,status,group')->count();
        $count22=$att->join('join sz_user on sz_user.num=sz_attendance.num')->field('name,date')->where('leader=1')->count();
        $info2=$att->join('join sz_user on sz_user.num=sz_attendance.num')->field('name,date')->where('leader=1')->select();
        for($count1=0; $count1<$count11; $count1++) {
            for($count2=0;$count2<$count22; $count2++)
            {
                if(($info1[$count1]['date']==$info2[$count2]['date']))
                {
                    $info1[$count1]['operator']=$info2[$count2]['name'];
                }
            }
        }
        $test->code=0;
        $test->mgs="";
        $test->count=$count11;
        $test->data=$info1;
        $this->ajaxReturn($test);
    }
    public function profile1($page,$limit)
    {
        $test=new Test();
        $att=M('attendance');
        $user=M('user');
        $id=session('id');
        $start=($page-1)*$limit;
        $count1=$att->join('join sz_user on sz_user.num=sz_attendance.num')->field('date,sz_attendance.num,name,stat,status,group')->where("sz_user.id='$id'")->count();
        $info1=$att->join('join sz_user on sz_user.num=sz_attendance.num')->field('date,sz_attendance.num,name,stat,status,group')->where("sz_user.id='$id'")->limit($start,$limit)->select();
        $info2=$att->join('join sz_user on sz_user.num=sz_attendance.num')->field('name')->where('leader=1')->limit($start,$limit)->select();
        for($count=0;$count<$count1;$count++)
        {
            //sleep(5);
            $info1[$count]['operator']=$info2[$count]['name'];
            //ob_flush();
        }
        $test->code=0;
        $test->mgs="";
        $test->count=$count1;
        $test->data=$info1;
        $this->ajaxReturn($test);
    }
    public function profile2()
    {
        $att=M('attendance');
        $user=M('user');
        $id=session('id');
        $info=$user->field('num')->where("id='$id'")->find();
        $num=$info['num'];
        $count1=$att->where("num='$num' and stat=1")->count();
        $count2=$att->where("num='$num' and stat=-1")->count();
        $count3=$att->where("num='$num' and stat=-2")->count();
        $test[]=new Test2();
        $test[0]->value=$count1;
        $test[1]->value=$count2;
        $test[2]->value=$count3;
        $test[0]->name='到勤';
        $test[1]->name='缺勤';
        $test[2]->name='迟到';
        $this->ajaxReturn($test);
    }
    public function profile3()
    {
        $att=M('attendance');
        $day=date('Y-m-d',time());
        $count1=$att->where("date='day' and stat=1")->count();
        $count2=$att->where("date='day' and stat=-1")->count();
        $count3=$att->where("date='day' and stat=-2")->count();
        $test[]=new Test2();
        $test[0]->value=$count1;
        $test[1]->value=$count2;
        $test[2]->value=$count3;
        $test[0]->name='到勤';
        $test[1]->name='缺勤';
        $test[2]->name='迟到';
        $this->ajaxReturn($test);
    }  
    public function tip()
    {
        $att=M('attendance');
        $user=M('user');
        $id=session('id');
        $info=$user->field('num')->where("id='$id'")->find();
        $num=$info['num'];
        $nfo1=$att->field('stat')->where("num='$num'")->find();
        $info2=$user->field('name')->where('duty=1')->find();
        if($info1['stat']=1)
        {
            $result[0]="已经考勤";
        }
        else if($info1['stat']=-1)
        {
            $result[0]="迟到";
        }
        else if($info1['stat']=-2)
        {
            $result[0]="未到勤";
        }
        else
        {
            $result[0]="未考勤";
        }
        $result[1]=$info2['name'];
        $this->ajaxReturn($result);
    }
    public function check()
    {
        $att=M('attendance');
        $day=date('Y-m-d',time());
        $info=$att->where("date='$day' and stat=0")->find();
        $this->ajaxReturn($info);
    }
}